package play

import kotlin.random.Random

class Lottery {

    private val redBall = intArrayOf(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33)
    private val blueBall = intArrayOf(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)

    fun generate() : MutableList<Int>{
        val list = mutableListOf<Int>()
        for(i in 0..5){
            var target = 0
            do {
                target = redBall[Random.nextInt(33)]
            }while (list.contains(target))
            list.add(target)
        }
        list.sort()
        list.add(blueBall[Random.nextInt(16)])
        return list
    }
}

/**
 * 高位在前，低位在后
 */
fun getTwoBytesInt(data : ByteArray, highIndex : Int, lowIndex : Int) : Int{
    val size = data.size
    if(highIndex < size && lowIndex < size){
        return (((data[highIndex].toInt()) shl 8) and 0xFF00) or ((data[lowIndex].toInt()) and 0x00FF)
    }
    return -1
}

fun main(args: Array<String>) {
//    statics()

//    println(generateSecondRewards(20200331))

    val br = byteArrayOf(1.toByte(), 1.toByte())
    println(getTwoBytesInt(br, 0, 1))
}

fun generateSecondRewards(stop : Int) : MutableList<Int>{
    var i = 0
    var r : MutableList<Int> = mutableListOf()
    while (i != stop){
        r = Lottery().generate()
        i++
    }
    return r
}

fun statics(){
    val s = listOf(12, 14, 15, 17, 29, 33, 12)
    val s2 = listOf(3, 6, 10, 11, 27,33, 3)

    var i = 0
    var j = 0
    var hasSecondReward = false;

    val target = s;
    val targetc = target.slice(IntRange(0, 5))
    var r = Lottery().generate()
    while (r != target){
        if(!hasSecondReward){
            val rc = r.slice(IntRange(0, 5))
            if(rc == targetc){
                hasSecondReward = true
                println("\n 中二等奖号码 ==> $targetc 需要摇 $j 次")
            }
            j++
        }
        r =  Lottery().generate()
        i++
    }
    println("\n 中一等奖号码 ==> $r 需要摇 $i 次")

}
